home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / ScreenDump / DumpScreen.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.6 KB  |  143 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        DumpScreen.c
  3.  
  4.     Contains:    This snippet shows how to dump an area of the screen.
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 19891999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include <QDOffscreen.h>
  25. #include <PictUtils.h>
  26. #include <Windows.h>
  27.  
  28. #ifndef topLeft
  29. #define topLeft(r)                      (((Point *) &(r))[0])
  30. #endif
  31.  
  32. #ifndef botRight
  33. #define botRight(r)                     (((Point *) &(r))[1])
  34. #endif
  35.  
  36. PicHandle DumpScreenArea();
  37.  
  38. // dump an area of the screen
  39. // return a PicHandle or nil
  40. PicHandle DumpScreenArea()
  41. {
  42.     CGrafPtr    wPort ;
  43.     CGrafPtr    savedPort ;
  44.     GDHandle    oldDevice ;
  45.     PicHandle    thePict = nil ;
  46.     
  47.     GWorldPtr    theNewWorld ;
  48.     GDHandle    gDevice;
  49.     
  50.     Rect        gDeviceRect;
  51.     Rect        intersectingRect;
  52.     Point        anchorPt ;
  53.     
  54.     OSErr        theErr ;
  55.     Rect         area ;
  56.     
  57.     RgnHandle    grayRgn = GetGrayRgn() ;
  58.     Rect        wideOpenRect = (**grayRgn).rgnBBox ;
  59.     
  60.     Rect RubberBandIt(Point    anchorPt) ;
  61.  
  62.     // save our world
  63.     GetGWorld( &savedPort, &oldDevice ) ;
  64.         
  65.     // get the window managers grafport
  66.     GetCWMgrPort( &wPort ) ;
  67.  
  68.     // and make the window managers grafport our current port
  69.     SetPort( (GrafPtr)wPort ) ;
  70.  
  71.     ClipRect( &wideOpenRect ) ;
  72.     
  73.     while (!Button())
  74.         GetMouse(&anchorPt);        // get the current mouse pos.
  75.         
  76.     area = RubberBandIt(anchorPt) ;
  77.  
  78.     // set up a deep - 32 bit - GWorld (you don't have to use GWorld stuff, though,
  79.     // see forrest tanaka's principia of offscreen tech note in the 
  80.     // imaging/graphics technotes for alternative methods).
  81.     // we do this to keep things simple
  82.     
  83.     if((theErr = NewGWorld( &theNewWorld, 32, &area, nil, nil, 0L )) != noErr)
  84.         return nil ;
  85.     
  86.     SetGWorld( (CGrafPtr)theNewWorld, nil ) ;
  87.  
  88.     // Get the handle to the first device in the global device list.
  89.     // loop through the device list, checking if the rect defined by area 
  90.     // intersects the device
  91.     
  92.     for( gDevice = GetDeviceList(); gDevice != nil; gDevice = GetNextDevice( gDevice )) {
  93.         
  94.         // Get the device's gdRect and convert it to local coordinates.
  95.         gDeviceRect = (**gDevice).gdRect;
  96.             
  97.         GlobalToLocal( &topLeft( gDeviceRect ) );
  98.         GlobalToLocal( &botRight( gDeviceRect ) );
  99.         
  100.         // Check if the app's window rect intersects the device's, and if it
  101.         // does, set the clip region's rect to the intersection and copy into
  102.         // our offscreen buffer
  103.         
  104.         if (SectRect( &area, &gDeviceRect, &intersectingRect ))
  105.         {
  106.             ClipRect( &intersectingRect );
  107.             CopyBits( (BitMap *)(*(**gDevice).gdPMap), 
  108.                       (BitMap *)(*(*theNewWorld).portPixMap), 
  109.                       &intersectingRect, 
  110.                       &intersectingRect, 
  111.                       srcCopy, 
  112.                       nil );
  113.         }
  114.     }
  115.     
  116.     SetGWorld( (CGrafPtr)theNewWorld, nil ) ;
  117.     
  118.     thePict = OpenPicture( &area );
  119.     if(thePict != nil ) {
  120.     
  121.         ClipRect( &area );
  122.         
  123.         CopyBits( (BitMap *)(*(*theNewWorld).portPixMap), 
  124.                   (BitMap *)(*(*theNewWorld).portPixMap), 
  125.                   &area, 
  126.                   &area, 
  127.                   srcCopy, 
  128.                   nil );
  129.                   
  130.         ClosePicture();
  131.  
  132.     }
  133.     
  134.     FlushEvents( mDownMask, 0 ) ;
  135.     
  136.     // restore the world
  137.     SetGWorld( savedPort, oldDevice ) ;
  138.     DisposeGWorld( theNewWorld );
  139.     
  140.     // return the pict
  141.     return thePict ;
  142. }
  143.